package com.onlinevideo.service.impl;

import com.onlinevideo.dao.impl.HistoryDaoImpl;
import com.onlinevideo.entity.History;
import com.onlinevideo.service.HistoryService;

import java.util.List;

public class HistoryServiceImpl implements HistoryService {
    HistoryDaoImpl historyDao = new HistoryDaoImpl();

    @Override
    public int insert(History history) {
        return historyDao.insert(history);
    }

    @Override
    public int delete(Long id) {
        return historyDao.delete(id);
    }

    @Override
    public int deleteByUserId(Long userId) {
        return historyDao.deleteByUserId(userId);
    }

    @Override
    public int deleteByVideoId(Long videoId) {
        return historyDao.deleteByVideoId(videoId);
    }

    @Override
    public int update(History history) {
        return historyDao.update(history);
    }

    @Override
    public History queryById(Long id) {
        return historyDao.queryById(id);
    }

    @Override
    public List<History> queryAll() {
        return historyDao.queryAll();
    }

    @Override
    public List<History> queryByUser(Long id) {
        return historyDao.queryByUser(id);
    }

    @Override
    public List<History> queryByUserAndVideo(Long userId, Long videoId) {
        return historyDao.queryByUserAndVideo(userId, videoId);
    }

    @Override
    public int addHistory(Long userId, Long videoId) {
        String time = String.valueOf(System.currentTimeMillis());
        List<History> histories = historyDao.queryByUserAndVideo(userId, videoId);
        if (histories.size() > 0) {
            History history1 = histories.get(0);
            history1.setTime(time);
            return historyDao.update(history1);
        } else {
            History history = new History(null, userId, videoId, time);
            return historyDao.insert(history);
        }
    }
}
